package hlhj.fhp.tvlib.utils

import android.annotation.SuppressLint
import android.util.Log
import java.text.ParseException
import java.text.SimpleDateFormat
import java.util.*

/**
 * Created by Administrator on 2018\5\22 0022.
 */
object TimeUt {

    private val FIRST_DAY = Calendar.MONDAY
    private val dataList=ArrayList<String>()
    private val dataList2=ArrayList<String>()
     fun printWeekdays(): ArrayList<String> {
        val calendar = Calendar.getInstance()
        setToFirstDay(calendar)
        for (i in 0..6) {
            dataList.add(printDay(calendar))
            calendar.add(Calendar.DATE, 1)
        }
         return dataList
    }
     fun printWeekdays2(): ArrayList<String> {
        val calendar = Calendar.getInstance()
        setToFirstDay(calendar)
        for (i in 0..6) {
            dataList2.add(printDay2(calendar))
            calendar.add(Calendar.DATE, 1)
        }
         return dataList2
    }

    private fun setToFirstDay(calendar: Calendar) {
        while (calendar.get(Calendar.DAY_OF_WEEK) !== FIRST_DAY) {
            calendar.add(Calendar.DATE, -1)
        }
    }

    private fun printDay(calendar: Calendar) :String{
        val dateFormat = SimpleDateFormat("MM-dd")
        System.out.println(dateFormat.format(calendar.getTime()))
        return dateFormat.format(calendar.getTime())
    }
    private fun printDay2(calendar: Calendar) :String{
        val dateFormat = SimpleDateFormat("yyyy-MM-dd")
        System.out.println(dateFormat.format(calendar.getTime()))
        return dateFormat.format(calendar.getTime())
    }

    /**
     * 将一个时间戳转换成提示性时间字符串，如刚刚，1秒前
     *
     * @param timeStamp
     * @return
     */
    fun convertTimeToFormat(timeStamp: Long): String {
        var timeStamp = timeStamp
        val times = timeStamp.toString() + ""
        if (times.length >= 11) {
            timeStamp = java.lang.Long.parseLong(times) / 1000.toLong()
        }
        val curTime = System.currentTimeMillis() / 1000.toLong()

        val time = curTime - timeStamp

        return if (time < 60 && time >= 0) {
            "刚刚"
        } else if (time >= 60 && time < 3600) {
            (time / 60).toString() + "分钟前"
        } else if (time >= 3600 && time < 3600 * 24) {
            (time / 3600).toString() + "小时前"
        } else if (time >= 3600 * 24 && time < 3600 * 24 * 10) {
            (time / 3600 / 24).toString() + "天前"
        } else {
            timeStampToStr(timeStamp)
        }
    }

    /**
     * 时间戳转化为时间格式
     *
     * @param timeStamp
     * @return
     */
    fun timeStampToStr(timeStamp: Long): String {
        var timeStamp = timeStamp
        val times = timeStamp.toString()
        if (times.length >= 11) {
            timeStamp = java.lang.Long.parseLong(times) / 1000.toLong()
        }
        @SuppressLint("SimpleDateFormat") val sdf = SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
        return sdf.format(timeStamp * 1000)
    }

    /**
     * 获取前n天日期、后n天日期
     *
     * @param distanceDay 前几天 如获取前7天日期则传-7即可；如果后7天则传7
     * @return
     */
    fun getOldDate(distanceDay: Int): String {
        val dft = SimpleDateFormat("yyyy-MM-dd")
        val beginDate = Date()
        val date = Calendar.getInstance()
        date.time = beginDate
        date.set(Calendar.DATE, date.get(Calendar.DATE) + distanceDay)
        var endDate: Date? = null
        try {
            endDate = dft.parse(dft.format(date.time))
        } catch (e: ParseException) {
            e.printStackTrace()
        }

        Log.e("fhp","前7天==" + dft.format(endDate))
        return dft.format(endDate)
    }
}